<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Rsdemo extends Controller
{
  public function index()
  {
    // phpinfo();
    // echo './public/static';
    // echo APP_PATH ;
    // header('Content-type:image/gif');
    // $id = md5('qazwsxedcrfv1das1de1');
    // $img = imagecreatetruecolor(120, 40);
    // $bgcolor = imagecolorallocate($img, 28, 99, 220);
    // $fontColor = imagecolorallocate($img, 255, 255, 255);
    // imagefill($img, 0, 0, $bgcolor);
    // $font = ROOT_PATH . '/public/static/fonts/hans-nor.otf';
    // $text = substr($id, 0, 6);
    // imagettftext($img, 20, 0, 12, 30, $fontColor, $font, $text);
    // /* imagepng */($img); // 指定输出到实体路径就无法输出到浏览器！！！
    // // magepng($img， ROOT_PATH'/public/static/images'.$id.'.png'); 
    // ob_end_flush(); // 解决图片乱码问题
    // imagedestroy($img);
    // return $img;
    // return 123;
    if (false) {
      return 123;
    } else {
      return 321;
    }
  }

  public function getPoints(Request $request)
  {
    $belongTo = $request->param('belongTo');
    // return json($belongTo);
    try {
      // $res = Db::connect('rs')->name('measurepoint')->where(['belongTo' => $belongTo])->fetchSql()->select();
      // $res = Db::connect('rs')->name('measurepoint')->where(['belongTo' => $belongTo])->select();
     
      //$points = Db::connect('rs')->name('point')->query('select * from point order by name desc');
      
      $res = Db::connect('rs')->name('measurepoint')->query('SELECT t.* FROM (SELECT productPrototypeFeature,max(eventTime) as eventTime, txObserValue,tyObserValue,tzObserValue FROM measurepoint GROUP BY productPrototypeFeature) a LEFT JOIN measurepoint t ON t.productPrototypeFeature=a.productPrototypeFeature and t.eventTime=a.eventTime');
      // return json($res);
      return json($res);
    } catch (\Exception $e) {
      return json($e->getMessage());
    }
  }

  public function addPoint(Request $request)
  {
    $rec = $request->param();
    $id = substr(md5(uniqid(microtime(true), true)), 0, 8);
    $now = date('Y-m-d H:i:s');
    header('Content-type:image/gif');
    $img = imagecreatetruecolor(120, 40);
    $bgcolor = imagecolorallocate($img, 28, 99, 220);
    $fontColor = imagecolorallocate($img, 255, 255, 255);
    imagefill($img, 0, 0, $bgcolor);
    $font = ROOT_PATH . '/public/static/fonts/hans-nor.otf';
    $text = substr($id, 0, 8);
    imagettftext($img, 20, 0, 12, 30, $fontColor, $font, $text);
    // imagepng($img); // 指定输出到实体路径就无法输出到浏览器！！！
    $p = ROOT_PATH . '/public/static/images/' . $id . '.png';
    $f = imagepng($img, $p);
    ob_end_flush(); // 解决图片乱码问题
    imagedestroy($img);
    // $rec['positionXZY'] = 5.444;
    if ($f) {
      $arr = array(2, 5, 10, 20);
      try {
        foreach ($arr as $key => $val) {
          $res = Db::connect('rs')->name('measurepoint')->insert(['id' => $id . $key, 'imgPath' => $p, 'txObserValue' => rand(1, 21), 'tyObserValue' => rand(5, 12), 'tzObserValue' => rand(10, 16), 'multiple' => 1, 'eventTime' => $now, 'belongTo' => $rec['belongTo'], 'productPrototypeFeature' => $val]);
        }

        if ($res > 0) {
          return json(
            array(
              'code' => 0,
              'msg' => 'insert success',
              'count' => $res,
              'data' => array()
            )
          );
        } else {
          return json(array(
            'code' => 1,
            'msg' => 'error',
            'count' => $res,
            'data' => array()
          ));
        }
        return json($rec);
        //code...
      } catch (\Exception $e) { //  tp5 中 异常类继承了 php中的异常类  可以直接获取错误信息
        //throw $th;
        return json(array(
          'code' => 1,
          'msg' => $e->getMessage(),
        ));
      }
    } else {
      return json(array(
        'code' => 1,
        'msg' => '图片未导入到实体，请检查',
      ));
    }
  }
  public function addPoints(Request $request)
  {
    $rec = $request->param();
    $id = substr(md5(uniqid(microtime(true), true)), 0, 8);
    $now = date('Y-m-d H:i:s');
    header('Content-type:image/gif');
    $img = imagecreatetruecolor(120, 40);
    $bgcolor = imagecolorallocate($img, 28, 99, 220);
    $fontColor = imagecolorallocate($img, 255, 255, 255);
    imagefill($img, 0, 0, $bgcolor);
    $font = ROOT_PATH . '/public/static/fonts/hans-nor.otf';
    $text = substr($id, 0, 8);
    imagettftext($img, 20, 0, 12, 30, $fontColor, $font, $text);
    // imagepng($img); // 指定输出到实体路径就无法输出到浏览器！！！
    $p = ROOT_PATH . '/public/static/images/' . $id . '.png';
    $f = imagepng($img, $p);
    ob_end_flush(); // 解决图片乱码问题
    imagedestroy($img);
    // $rec['positionXZY'] = 5.444;
    if ($f) {
      $arr = array(2, 5, 10, 20);
      try {
        $res = Db::connect('rs')->name('point')->insert(['id' => $id, 'imgPath' => $p, 'positionX' => $rec['positionX'], 'positionY' => $rec['positionY'], 'positionZ' => $rec['positionZ'], 'multiple' => 1, 'createTime' => $now, 'belongTo' => $rec['belongTo'], 'name' => $rec['name'] ]);
        if ($res > 0) {
          return json(
            array(
              'code' => 0,
              'msg' => 'insert success',
              'count' => $res,
              'data' => array()
            )
          );
        } else {
          return json(array(
            'code' => 1,
            'msg' => 'error',
            'count' => $res,
            'data' => array()
          ));
        }
        return json($rec);
        //code...
      } catch (\Exception $e) { //  tp5 中 异常类继承了 php中的异常类  可以直接获取错误信息
        //throw $th;
        return json(array(
          'code' => 1,
          'msg' => $e->getMessage(),
        ));
      }
    } else {
      return json(array(
        'code' => 1,
        'msg' => '图片未导入到实体，请检查',
      ));
    }
  }

  public function queryPoint(Request $request) {
    $rec = $request->param();
    $res = Db::connect('rs')->name('measurepoint')->query("select * from measurepoint where name={$rec['name']} order by createTime desc limit 50 ");
    if (count($res) > 0) {
      return json($res);
    }else {
      return json('err in sql,please check!');
    }
    
  }
}
